package com.drcoffee.drcoffeemall.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sf.csim.express.service.CallExpressServiceTools;
import com.sf.csim.express.service.HttpClientUtil;
import com.sf.csim.express.service.IServiceCodeStandard;
import com.sf.csim.express.service.code.ExpressServiceCodeEnum;
import lombok.extern.slf4j.Slf4j;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/*
 * 顺丰接口调用工具类
 * */
@Slf4j
public class SfApiUtil {
    // 顾客编码
    private static final String CLIENT_CODE = "KBSKF3T1NAGI";  //此处替换为您在丰桥平台获取的顾客编码
    // 校验码
    private static final String CHECK_WORD = "wMuj3fErNspRPBQZ2Q2dFViDOAVQSNyk";//此处替换为您在丰桥平台获取的校验码
    // 生产请求地址
    private static final String CALL_URL_PROD = "https://sfapi.sf-express.com/std/service";



    /**
     * 路由查询接口接口-速运类API
     * 文档：<a href="http://qiao.sf-express.com/Api/ApiDetails?level3=397&interName=%E8%B7%AF%E7%94%B1%E6%9F%A5%E8%AF%A2%E6%8E%A5%E5%8F%A3-EXP_RECE_SEARCH_ROUTES">...</a>
     */
    public static JSONObject routeQuery(String sf_code) {
        try {
            IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("trackingType", "1");
            requestMap.put("trackingNumber", sf_code);
            requestMap.put("checkPhoneNo", "1178");
            ObjectMapper objectMapper = new ObjectMapper();
            String msgData = objectMapper.writeValueAsString(requestMap);
            log.info("路由查询接口接口-速运类API-请求报文：" + requestMap);
            JSONObject result = sendRequest(msgData, standardService);
            log.info("响应结果：" + result);
            return result;
        } catch (Exception e) {
            log.error("路由查询接口接口-速运类API-异常", e);
            return JSON.parseObject(e.getMessage());
        }
    }

    /**
     * 预计派送时间查询接口-速运类API
     * 文档：<a href="http://qiao.sf-express.com/Api/ApiDetails?level3=409&interName=%E9%A2%84%E8%AE%A1%E6%B4%BE%E9%80%81%E6%97%B6%E9%97%B4%E6%8E%A5%E5%8F%A3-EXP_RECE_SEARCH_PROMITM">...</a>
     */
    public static JSONObject estimatedDeliveryTime(String code) {
        try {
            IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_PROMITM;
            ArrayList<String> checkNosList = new ArrayList<>();
            checkNosList.add("17634611178");
            Map<String, Object> requestMap = new HashMap<>();
            requestMap.put("searchNo", code);
            requestMap.put("checkType", 1);
            requestMap.put("checkNos", checkNosList);
            System.out.println(requestMap);
            ObjectMapper objectMapper = new ObjectMapper();
            String msgData = objectMapper.writeValueAsString(requestMap);
            log.info("预计派送时间查询接口-速运类API-请求报文：" + requestMap);
            JSONObject result = sendRequest(msgData, standardService);
            log.info("响应结果：" + result);
            return result;
        } catch (Exception e) {
            log.error("预计派送时间查询接口-速运类API-异常", e);
            return JSON.parseObject(e.getMessage());
        }
    }

    /**
     * 运单号合法性校验接口-速运类API
     * 文档：<a href="http://qiao.sf-express.com/Api/ApiDetails?level3=408&interName=%E8%BF%90%E5%8D%95%E5%8F%B7%E5%90%88%E6%B3%95%E6%80%A7%E6%A0%A1%E9%AA%8C%E6%8E%A5%E5%8F%A3-EXP_RECE_VALIDATE_WAYBILLNO">...</a>
     */
    public static JSONObject codeValidityCheck(String code) {
        try {
            IServiceCodeStandard standardService = ExpressServiceCodeEnum.EXP_RECE_SEARCH_ROUTES;
            Map<String, String> requestMap = new HashMap<>();
            requestMap.put("waybillNo", code);
            ObjectMapper objectMapper = new ObjectMapper();
            String msgData = objectMapper.writeValueAsString(requestMap);
            log.info("运单号合法性校验接口-速运类API-请求报文：" + requestMap);
            JSONObject result = sendRequest(msgData, standardService);
            log.info("响应结果：" + result);
            return result;
        } catch (Exception e) {
            log.error("运单号合法性校验接口-速运类API-异常", e);
            return JSON.parseObject(e.getMessage());
        }
    }

    public static void main(String[] args) {
        estimatedDeliveryTime("SF1605831662775");

    }

    /*
     * 发送请求
     * */
    public static JSONObject sendRequest(String msgData, IServiceCodeStandard standardService) throws UnsupportedEncodingException {
        Map<String, String> params = new HashMap<>();
        params.put("partnerID", CLIENT_CODE);  // 顾客编码 ，对应丰桥上获取的clientCode
        params.put("requestID", UUID.randomUUID().toString().replace("-", ""));
        params.put("serviceCode", standardService.getCode());// 接口服务码
        String timeStamp = String.valueOf(System.currentTimeMillis());
        params.put("timestamp", timeStamp);
        params.put("msgData", msgData);
        params.put("msgDigest", CallExpressServiceTools.getMsgDigest(msgData, timeStamp, CHECK_WORD));
        log.info("调用实际请求：" + params);
        String result = HttpClientUtil.post(CALL_URL_PROD, params);
        return JSON.parseObject((String) JSON.parseObject(result).get("apiResultData"));
    }
}
